//
//  InformationViewController.m
//  Rail
//
//  Created by hychen on 2018/2/25.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "InformationViewController.h"
#import "UINavigationController+FDFullscreenPopGesture.h"

#import "InfomationHeaderView.h"
#import "InformationTableViewCell.h"

#import "WarnSettingViewController.h"
#import "AllWarnViewController.h"
#import "TodoWarnViewController.h"
#import "NoticeWarnViewController.h"
#import "HandleWarnViewController.h"
#import "TodoWarnUnViewController.h"
#import "WarnDetailViewController.h"
#import "WarnBaseViewController.h"
#import "CurrentWarnListViewController.h"

@interface InformationViewController ()<UINavigationControllerDelegate>
@property (nonatomic, strong) InformationAlertModel *alertModel;
@property (nonatomic, strong) InfomationHeaderView *headerView;
@end

@implementation InformationViewController

- (void)dealloc
{
    self.navigationController.delegate = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setFd_prefersNavigationBarHidden:YES];
    
    self.view.backgroundColor = Color_Background;
    
    self.navigationController.delegate = self;
    
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.view);
        make.height.mas_equalTo(SCREEN_HEIGHT-[BMUtility tabbarHeight]);
    }];
    
    [self prepareTableHeaderView];
    
    [self.tableView.mj_header beginRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)prepareTableHeaderView {
    
    BMWeakSelf
    InfomationHeaderView *headerView = [InfomationHeaderView headerView];
    headerView.model = self.alertModel;
    self.headerView = headerView;
    
    //设置 ()
//    [headerView setBlockClickSetting:^{
//        [weakSelf gotoSettingController];
//    }];
    
    //当前报警记录
    [headerView setBlockClickCurrentWarn:^{
        [weakSelf gotoCurrentWarnController];
    }];
    
    //未处理警报
    [headerView setBlockClickTodoWarn:^{
        [weakSelf gotoTodoWarnController];
    }];
    
    //全部报警
    [headerView setBlockClickAllWarn:^{
        [weakSelf gotoAllWarnController];
    }];
    
    //公告通知
    [headerView setBlockClickNotice:^{
        [weakSelf gotoNoticeWarnController];
    }];
    
    self.tableView.tableHeaderView = headerView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WarnDetailModel *model = [self.dataArray objectAtIndex:indexPath.row];
    InformationTableViewCell *cell = [InformationTableViewCell cellWithTableView:tableView class:[InformationTableViewCell class]];
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    WarnDetailModel *model = [self.dataArray objectAtIndex:indexPath.row];
    WarnDetailViewController *vc = [WarnDetailViewController new];
    BMWeakSelf
    [vc setBlockProcessedWarn:^{
        [weakSelf.tableView.mj_header beginRefreshing];
    }];
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90.f;
}

#pragma mark - 跳转
//- (void)gotoSettingController {
//    WarnSettingViewController *settingVc = [WarnSettingViewController new];
//    settingVc.hidesBottomBarWhenPushed = YES;
//    [[BaseNavigationController rootNavigationController] pushViewController:settingVc animated:YES];
//}

- (void)gotoCurrentWarnController {
    CurrentWarnListViewController *vc = [CurrentWarnListViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)gotoTodoWarnController {
    WarnBaseViewController *vc = [WarnBaseViewController new];
    vc.type = WarnRequestDataTypeTodo;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)gotoAllWarnController {
    AllWarnViewController *vc = [AllWarnViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)gotoNoticeWarnController {
    NoticeWarnViewController *vc = [NoticeWarnViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 网络
- (void)refreshData {
    //获取头部数据
    BMWeakSelf
    [HttpRequest loadInformationWithAlertStatisticSuccess:^(InformationAlertModel *object) {
        weakSelf.alertModel = object;
        [weakSelf prepareTableHeaderView];
        [weakSelf.tableView reloadData];
    } failure:^(NSString *code, NSString *msg) {
        Show_SV_Error_Msg(msg)
    }];
    
    //获取列表数据
    //暂定获取当天警报
    self.isOpenFooter = NO;
    self.currentPage = 1;
    [HttpRequest loadCurrentWarnListDataWithPageNo:[@(self.currentPage) stringValue] success:^(NSArray *object) {
        weakSelf.dataArray = [NSMutableArray arrayWithArray:object?:@[]];
        if (object.count > 9) {
            weakSelf.isOpenFooter = YES;
        }
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.mj_header endRefreshing];
    } failure:^(NSString *code, NSString *msg) {
        [weakSelf.tableView.mj_header endRefreshing];
        Show_Msg_Error(msg)
    }];
}

- (void)loadMoreData {
    self.currentPage++;
    BMWeakSelf
    [HttpRequest loadCurrentWarnListDataWithPageNo:[@(self.currentPage) stringValue] success:^(NSArray *object) {
        if ([object isKindOfClass:[NSArray class]] && object.count > 0) {
            [weakSelf.dataArray addObjectsFromArray:object];
            [weakSelf.tableView.mj_footer endRefreshing];
            [weakSelf.tableView reloadData];
        }
        else {
            [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
        }
    } failure:^(NSString *code, NSString *msg) {
        Show_Msg_Error(msg)
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
}


#pragma mark - UINavigationControllerDelegate
// 将要显示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自己
    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
    
    [self.navigationController setNavigationBarHidden:isShowHomePage animated:NO];
}
@end
